function [commonGeom,textExtent] = DSGetCommonGeometry

%DSGETCOMMONGEOMETRY Summary of this function goes here
%
%   DSGETCOMMONGEOMETRY(varagin) Detailed explanation goes here
%
%   Inputs:
%
%
%   Outputs:
%
%
%   Syntax:
%
%
%   Example:
%
%
%   See also:
%
% 
%   Author(s): Xiaobiao Huang
%   Copyright 2013-2020 The Xiaobiao Studio.
%   $Revision: 1.0.0.0 $  $Date: 07-Jul-2013 13:48:50 $


% function [commonGeom,textExtent] = ds_get_common_geometry

% DS_GET_COMMON_GEOMETRY creates common geometry data for dSPACE dialogs.
%
% SYNTAX [commonGeom,textExtent] = ds_get_common_geometry
%   creates common geometry data for dSPACE dialogs
%
%   INPUT ARGUMENTS
%   - / -
%
%   OUTPUT ARGUMENTS
%   commonGeom        MATLAB struct with various sizes of uicontrols and fonts
%   textExtent        A text uicontrol which can be used to measure the extents
%                     of strings in GUIs
%
% EXAMPLES  % get extent of string 'Hello world'
%   [commonGeom,textExtent] = ds_get_common_geomometry
%   set(textExtent, 'String', 'Hello world');
%   ext = get(textExtent, 'Extent');
%
% REMARKS 
%   This function creates a persistent hidden figure window for the "textExtent"
%   uicontrol. To increase performance "textExtent" should not be deleted.


% Author(s): D. Andoleit, T. Pietzsch
% $RCSfile: ds_get_common_geometry.m $
% $ProjectName: e:/ARC/Components/HostSW/ProdCodeGen/DialogHandler/Sources/MSrc/project.pj $
% $Revision: 1.5 $
% $Date: 2011/03/17 09:14:34MEZ $
%
% Copyright ?2011 dSPACE GmbH. All rights reserved.


persistent DS_GEOMOBJECTS_DATA

% early return if data is already available, and textextent is a valid handle
if ~isempty(DS_GEOMOBJECTS_DATA)
    if ishandle(DS_GEOMOBJECTS_DATA.textExtent),
        commonGeom = DS_GEOMOBJECTS_DATA.commonGeom;
        textExtent = DS_GEOMOBJECTS_DATA.textExtent;
        return;
    end
end

% create hidden figure window to hold the uicontrol for measurement of text extents
if isempty(DS_GEOMOBJECTS_DATA) | ~ishandle(DS_GEOMOBJECTS_DATA.fig)
    DS_GEOMOBJECTS_DATA.fig = figure( ...
        'Name',             'DS_GEOMOBJECTS_PRIVATE_FIGURE', ...
        'Tag',              'DS_GEOMOBJECTS_PRIVATE_FIGURE', ...
        'HandleVisibility', 'off', ...
        'Visible',          'off' ...
        );
end

% create uicontrol for text extent measurements
commonGeom.fontsize = get(0, 'FactoryUicontrolFontSize');
commonGeom.fontname = get(0, 'FactoryUicontrolFontName');
textExtent = uicontrol( ...
    'Parent',     DS_GEOMOBJECTS_DATA.fig, ...
    'Visible',    'off', ...
    'Style',      'text', ...
    'FontSize',   commonGeom.fontsize, ...
    'FontName',   commonGeom.fontname);

% get geometry struct for current platform
sysOffsets = i_UIGeom;

% Define generic font characterists.
calibrationStr = '_Revert_';
set(textExtent, 'String', calibrationStr);
ext = get(textExtent, 'Extent');

% standard button width and character height
sysButtonWidth = ext(3);
charHeight     = ext(4);

% prepare geometry struct
commonGeom.textHeight            = charHeight;
commonGeom.editHeight            = charHeight + sysOffsets.edit(4);
commonGeom.checkboxHeight        = charHeight + sysOffsets.checkbox(4);
commonGeom.radiobuttonHeight     = charHeight + sysOffsets.radiobutton(4);
commonGeom.popupHeight           = charHeight + sysOffsets.popupmenu(4);
commonGeom.sysButtonHeight       = (charHeight * 1.3) + sysOffsets.pushbutton(4);
commonGeom.sysButtonWidth        = sysButtonWidth + sysOffsets.pushbutton(3);
commonGeom.sysButtonDelta        = 8;
commonGeom.sheetSideEdgeBuffer   = 3;
commonGeom.sheetTopEdgeBuffer    = (commonGeom.textHeight/1.3);
commonGeom.sheetBottomEdgeBuffer = 5;
commonGeom.frameBottomEdgeBuffer = 8;
commonGeom.frameTopEdgeBuffer    = (commonGeom.textHeight/1.3) + 5;
commonGeom.frameSideEdgeBuffer   = 8;
commonGeom.frameDelta            = (commonGeom.textHeight/1.3);
commonGeom.figBottomEdgeBuffer   = commonGeom.textHeight+8;
commonGeom.figTopEdgeBuffer      = 5;
commonGeom.figSideEdgeBuffer     = 5;
commonGeom.sysButton_SheetDelta  = 5;
commonGeom.bigIconButtonSize     = [20 20];
commonGeom.smallIconButtonSize   = [16 16];
commonGeom.figColor              = [192 192 192]./255;

% commonGeom.bottomSheetOffset = ...
%     commonGeom.figBottomEdgeBuffer    + ...
%     commonGeom.sysButtonHeight        + ...
%     commonGeom.sysButton_SheetDelta; 

commonGeom.bottomSheetOffset = 5;

commonGeom.sysOffsets = sysOffsets;
commonGeom.textExtent = textExtent;

% set default values of treeview geometry
commonGeom.treeviewHeight      = 78 + charHeight * 11;
commonGeom.treeviewWidth       = sysButtonWidth * 3.6 + sysOffsets.listbox(3);

% save data to cache 
DS_GEOMOBJECTS_DATA.commonGeom = commonGeom;
DS_GEOMOBJECTS_DATA.textExtent = textExtent;


%************************************************************************
% Return geometry struct                                             ***
%************************************************************************

function outGeom = i_UIGeom()

fudgeFactors = i_CreateGeomStruct(computer);

outGeom.pushbutton      =  fudgeFactors(1,:);
outGeom.radiobutton     =  fudgeFactors(2,:);
outGeom.checkbox        =  fudgeFactors(3,:);
outGeom.edit            =  fudgeFactors(4,:);
outGeom.text            =  fudgeFactors(5,:);
outGeom.slider          =  fudgeFactors(6,:);
outGeom.frame           =  fudgeFactors(7,:);
outGeom.listbox         =  fudgeFactors(8,:);
outGeom.popupmenu       =  fudgeFactors(9,:);
outGeom.listboxHscroll  =  fudgeFactors(10,:);
outGeom.listboxVscroll  =  fudgeFactors(11,:);


%************************************************************************
% Function - create goemetry factors for given computer type.         ***
% Matrix rows are as shown in main function in assignment of          ***
%   outGeom.                                ***
% Matrix colums are: [0 0 width height]                  ***
%   measured as the number of extra pixels used by system decoration  ***
%************************************************************************

function geom = i_CreateGeomStruct(sys)

stdUnixPlatforms = {
    'SOL2', 'SUN4', 'HP700', 'SGI', 'SGI64', 'IBM_RS', 'DEC_RISC', 'VAX_VMSD', ...
        'VAX_VMSG', 'AXP_VMSIEEE', 'AXP_VMSG', 'ALPHA', 'HP300', 'CRAY', 'CONVEX', ...
        'LNX86' };

switch(sys),
    
    case {'PCWIN','PCWIN64'}
        geom = [
            0  0  4  4   %pushbutton
            0  0  18 0   %radiobutton
            0  0  18 0   %checkbox
            0  0  7  4   %edit
            0  0  0  0   %text
            0  0  0  0   %slider
            0  0  2  2   %frame
            0  0  16 2   %listbox
            0  0  20 4   %popupmenu
            0  0  0  18  %height of horizontal listbox scroll bar
            0  0  0  0   %width of vert listbox scroll bar (not needed)
        ];
        
    case stdUnixPlatforms,
        geom = [
            0  0  4  4   %pushbutton
            0  0  26 4   %radiobutton
            0  0  26 4   %checkbox
            0  0  4  4   %edit
            0  0  0  0   %text
            0  0  0  0   %slider
            0  0  4  4   %frame
            0  0  4  4   %listbox
            0  0  26 6   %popupmenu
            0  0  0  17  %height of horizontal listbox scroll bar
            0  0  19 0   %width of vertical listbox scroll bar
        ];
        
    case 'MAC2',
        geom = [
            0  0  6   6   %pushbutton
            0  0  18  0   %radiobutton
            0  0  18  4   %checkbox
            0  0  4   4   %edit
            0  0  0   0   %text
            0  0  0   0   %slider
            0  0  2   2   %frame
            0  0  18  2   %listbox
            0  0  30  3   %popupmenu
            0  0  0   17  %height of horizontal listbox scroll bar
            0  0  0   0   %width of vert listbox scroll bar (not needed)
        ];
        
    otherwise,
        error(['Invalid computer type ', sys, ' in ds_geomobjects.m' ]);  
        
end


% end % end of function  DSGetCommonGeometry
